<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">

      <el-form-item label="客户名称" prop="jcCustomerName">
        <el-input v-model="queryParams.jcCustomerName" placeholder="请输入费用类型" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="费用类型" prop="jcFeeTypeId">
        <el-input v-model="queryParams.jcFeeTypeId" placeholder="请输入费用类型" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="台账类型" prop="category">
        <el-input v-model="queryParams.category" placeholder="请输入台账类型" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="确认时间" prop="affirmTime">
        <el-date-picker clearable v-model="queryParams.affirmTime" type="date" value-format="yyyy-MM-dd"
          placeholder="请选择确认时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['ledger:detail:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['ledger:detail:export']">导出</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" :disabled="single" @click="handleConfirm"
                   v-hasPermi="['ledger:detail:edit']">确认</el-button>
      </el-col>

      <el-col :span="5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" :disabled="single" @click="handleUnconfirm"
                   v-hasPermi="['ledger:detail:edit']">反确认</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-radio-group v-model="queryParams.isConfirm" style="margin-bottom: 30px;" size="mini" @input="getList">
          <el-radio-button v-for="dict in dict.type.ConfirmStatus" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio-button>
        </el-radio-group>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="detailList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" fixed/>
      <el-table-column label="客户名称" align="left" prop="jcCustomerName" width="200"/>
      <el-table-column label="订单(运单)编号" align="center" width="130">
        <template #default="scope">
          <el-link :underline="false" type="primary" v-if="scope.row.jcOrderId">{{ scope.row.jcOrderCode }}</el-link>
          <el-link :underline="false" type="primary" v-else>{{ scope.row.jcShipmentCode }}</el-link>
        </template>
      </el-table-column>
      <el-table-column label="费用类型" align="center" prop="jcFeeTypeName"/>
      <el-table-column label="收入成本确认" align="center" width="100">
        <template #default="scope">
          <dict-tag :options="dict.type.ConfirmStatus" :value="scope.row.isConfirm || 0" />
        </template>
      </el-table-column>
      <el-table-column label="金额(税金/税率)" align="center" prop="amount" width="130">
        <template #default="scope">
          <span>{{ scope.row.amount }}({{ scope.row.input }}/{{ scope.row.taxrate }})</span>
        </template>
      </el-table-column>
      <el-table-column label="台账类型" align="center" prop="category" width="100">
        <template #default="scope">
          <dict-tag :options="dict.type.LedgerCost" :value="scope.row.category" />
        </template>
      </el-table-column>
      <el-table-column label="单据类型" align="center" prop="type">
        <template #default="scope">
          <span v-if="scope.row.type === 0">订单</span>
          <span v-if="scope.row.type === 1">运单</span>
          <span v-if="scope.row.type === 2">派车单</span>
          <span v-if="scope.row.type === 3">其他费用</span>
          <span v-if="scope.row.type === 4">费用传递</span>
        </template>
      </el-table-column>
      <el-table-column label="核销(未核销)金额(元)" align="center" prop="hxmoney" width="100">
        <template #default="scope">
          <span>{{ scope.row.hxmoney }}({{ scope.row.whmoney }})</span>
        </template>
      </el-table-column>
      <el-table-column label="总费用(元)" align="center" prop="zFy" width="100" />
      <el-table-column label="总干线运费(元)" align="center" prop="zGxyf" width="100" />
      <el-table-column label="总其他费用(元)" align="center" prop="zQtfy" width="100" />
      <el-table-column label="收入(元)" align="center" prop="income" width="100" />
      <el-table-column label="成本(元)" align="center" prop="outcome" width="100" />
      <el-table-column label="确认时间" align="center" prop="affirmTime" width="180" />
      <el-table-column label="收款方式" align="center" prop="collectMethod" width="100">
        <template #default="scope">
          <dict-tag :options="dict.type.CollectMethod" :value="scope.row.collectMethod" />
        </template>
      </el-table-column>
      <el-table-column label="结算方式" align="center" prop="settlementMethod" width="100">
        <template #default="scope">
          <dict-tag :options="dict.type.Clearing" :value="scope.row.settlementMethod" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="120">
        <template #default="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-if="scope.row.isConfirm === 0" v-hasPermi="['ledger:detail:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-if="scope.row.isConfirm === 0" v-hasPermi="['ledger:detail:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改客户台帐对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="金额" prop="amount">
              <el-input v-model="form.amount" placeholder="请输入金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="税金" prop="input">
              <el-input v-model="form.input" placeholder="请输入税金" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="费用类型" prop="jcFeeTypeId">
              <el-input v-model="form.jcFeeTypeId" placeholder="请输入费用类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="台账类型" prop="category">
              <el-input v-model="form.category" placeholder="请输入台账类型" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="核销金额" prop="hxmoney">
              <el-input v-model="form.hxmoney" placeholder="请输入核销金额" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总费用" prop="zFy">
              <el-input v-model="form.zFy" placeholder="请输入总费用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总干线运费" prop="zGxyf">
              <el-input v-model="form.zGxyf" placeholder="请输入总干线运费" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总其他费用" prop="zQtfy">
              <el-input v-model="form.zQtfy" placeholder="请输入总其他费用" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总送货费" prop="zShf">
              <el-input v-model="form.zShf" placeholder="请输入总送货费" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="总提货费" prop="zThf">
              <el-input v-model="form.zThf" placeholder="请输入总提货费" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收入" prop="income">
              <el-input v-model="form.income" placeholder="请输入收入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="成本" prop="outcome">
              <el-input v-model="form.outcome" placeholder="请输入成本" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="确认时间" prop="affirmTime">
              <el-date-picker clearable v-model="form.affirmTime" type="date" value-format="yyyy-MM-dd"
                placeholder="请选择确认时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收入成本确认" prop="isConfirm">
              <el-input v-model="form.isConfirm" placeholder="请输入收入成本确认(确认1,未确认0)" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="收款方式" prop="collectMethod">
              <el-input v-model="form.collectMethod" placeholder="请输入收款方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="支付方式" prop="payMethod">
              <el-input v-model="form.payMethod" placeholder="请输入支付方式" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="结算方式" prop="settlementMethod">
              <el-input v-model="form.settlementMethod" placeholder="请输入结算方式" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {customerList, getDetail, delDetail, addDetail, updateDetail, unconfirmed, confirm} from "@/api/ledger/detail";

export default {
  name: "IncomeIndex",
  dicts: ['LedgerCost', 'PayMethod', 'Clearing', 'CollectMethod', 'ConfirmStatus'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户台帐表格数据
      detailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        jcCustomerName: null,
        createName: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        occurrenceTime: null,
        taxrate: null,
        category: null,
        type: null,
        affirmTime: null,
        isConfirm: 0,
        collectMethod: null,
        payMethod: null,
        settlementMethod: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询客户台帐列表 */
    getList() {
      this.loading = true;
      customerList(this.queryParams).then(response => {
        this.detailList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        createName: null,
        createTime: null,
        description: null,
        modifyName: null,
        modifyTime: null,
        name: null,
        status: null,
        amount: null,
        explain1: null,
        explain2: null,
        explain3: null,
        input: null,
        occurrenceTime: null,
        taxrate: null,
        jcFeeTypeId: null,
        jcLedgerId: null,
        category: null,
        type: null,
        jcOrderId: null,
        jcPrescoId: null,
        jcShipmentId: null,
        jcDispatchId: null,
        jcFeeSeedId: null,
        jcOtherfeeId: null,
        arrivePay: null,
        backPay: null,
        hxmoney: null,
        monthPay: null,
        nowPay: null,
        whmoney: null,
        cashAdvances: null,
        oilPrepaid: null,
        yuE: null,
        zFy: null,
        zGxyf: null,
        zQtfy: null,
        zShf: null,
        zThf: null,
        yajin: null,
        yfYouka: null,
        income: null,
        outcome: null,
        jcShipmentTemplateId: null,
        jcCarrierId: null,
        affirmTime: null,
        isConfirm: null,
        collectMethod: null,
        payMethod: null,
        settlementMethod: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams.isConfirm =
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户台帐";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getDetail(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改客户台帐";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateDetail(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDetail(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除客户台帐编号为"' + ids + '"的数据项？').then(function () {
        return delDetail(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleConfirm(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认该费用项？').then(function () {
        return confirm(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    handleUnconfirm(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否反确认该费用项？').then(function () {
        return unconfirmed(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ledger/detail/export', {
        ...this.queryParams
      }, `detail_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
